package com.yoo.ons.base.adpater

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

abstract class BaseRecyclerAdapter<T, V : ViewBinding>(val inflaterBlock: (LayoutInflater) -> V) :
    RecyclerView.Adapter<BaseRecyclerAdapter.VH<V>>() {
    private var dataList = arrayListOf<T>()


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH<V> {
        val binding: V = inflaterBlock(LayoutInflater.from(parent.context))
        return VH(binding)
    }

    override fun onBindViewHolder(holder: VH<V>, position: Int) {
//        val params: FrameLayout.LayoutParams = FrameLayout.LayoutParams(
//            ViewGroup.LayoutParams.MATCH_PARENT,
//            ViewGroup.LayoutParams.WRAP_CONTENT
//        )
//        holder.binding.root.layoutParams = params
        onBindView(holder, position, dataList[position])
    }

    abstract fun onBindView(holder: VH<V>, position: Int, item: T?)

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun setDataList(data: ArrayList<T>){
        this.dataList = data
    }
    fun getDataList(): ArrayList<T> {
        return dataList
    }

    fun getItemPosition(data: T): Int {
        return dataList.indexOf(data)
    }

    fun addAll(data: ArrayList<T>) {
        dataList.addAll(data)
        notifyDataSetChanged()
    }

    class VH<V : ViewBinding>(val binding: V) : RecyclerView.ViewHolder(binding.root)

}